<?php
/*
Airmon convertor mysql to image (5GHz)
Author: FFAMax ffamax _ gmail.com

*/
require_once ('../config.php');
require_once ('../../ffamax-code-samples/functions.php');
{ //MySQL
	$mysql_link = mysql_pconnect(MYSQL_HOST, MYSQL_USERNAME, MYSQL_PASSWORD);
	if (!$mysql_link) {
		die('Could not connect: ' . mysql_error());
	}
	$db_selected = mysql_select_db(MYSQL_DATABASE);
	if (!$db_selected) {
		die ('Can\'t use BD : ' . mysql_error());
	}	
}

	$freq_low = 4900000000; 								// low freq: 	4 900 000 000
	$freq_hi = 6400000000;									// hight freq:	6 400 000 000
	$freq_grade = ($freq_hi-$freq_low) / 4800;
	
	$freq_box_font_size = 8;										// size of freq box size at bottom of image
	$time_box_font_size = 8;

	//header('Content-Type: image/png');
	$font_file = '../../ffamax-code-samples/fonts/TAHOMA.TTF';
			
	while (true){
		$sql = "SELECT UNIX_TIMESTAMP(time) as time , data FROM " . MYSQL_TABLE . " WHERE time > DATE_SUB(CURRENT_TIMESTAMP,  INTERVAL 1 HOUR) limit 2000";
		$time_start = microtime_float();		// timer start
		$result = mysql_query($sql);
		$time_end = microtime_float();			// timer end
		$time_run = $time_end - $time_start;
		echo "mysql query time: " . $time_run . PHP_EOL;
		if (!$result) {
			$result = mysql_query($sql);
			if (!$result) {
				echo('Invalid query or broken connection: ' . mysql_error() . PHP_EOL . $sql . PHP_EOL);
				die("program terminated" . PHP_EOL);
			}
		}
		$num_rows = mysql_num_rows($result);
		$y=0;
		$image = imagecreatetruecolor (4800,$num_rows);
		imagealphablending($image, true);
		$color_white = imagecolorallocate($image, 255, 255, 255);	
		{ // 1pixel lines
			$white_horizontal_line = imagecreatetruecolor (4800,1);
			$color_white_whl = imagecolorallocate($white_horizontal_line, 255, 255, 255);
			imagefill ($white_horizontal_line, 0, 0, $color_white_whl);
			$white_vertical_line = imagecreatetruecolor (10,$num_rows);
			$color_white_hhl = imagecolorallocate($white_vertical_line, 255, 255, 255);
			imagefill ($white_vertical_line, 0, 0, $color_white_hhl);
		}

		//imagesavealpha($image, true);
		$last_frame = NULL;
		$last_time = 0;
		while ($row = mysql_fetch_row($result)) {
			$in_mess = $row[1];
			if (strpos($in_mess,'FRAME: ') === false) {
				die("no answer" . __LINE__);
			}
			$data = str_replace('FRAME: ', '',$in_mess);
			$data_array = explode (',', $data);
			
			if ($last_frame === $data_array[0]) {
				echo "something go wrong " . __LINE__;
			}
			else {
				$last_frame = $data_array[0];
				if (count($data_array) === 4801) {
					for ($x=1;$x<=4800;$x++){
						$color = wireless_signal_colorate ($data_array[$x], $image);
						imagesetpixel ($image, $x-1,$y, $color);
					}
							if ((($row[0] - $last_time >= 60) OR ($row[0] % 60 === 0)) AND ($last_time !== $row[0])) {
								if ($y > $time_box_font_size * 3) {
									//imagefttext($image, 7, 0, 2, $y, $color_white, $font_file, date("Y-m-d H:i:s T"));
									imagecopymerge ($image, $white_horizontal_line, 0,$y, 0, 0, 4800, 1, 30);
									if (($num_rows - $y) > $freq_box_font_size+5) {
										$img_box_with_text = make_box_with_text (array(0,0,0), array(255,255,255), $font_file, $time_box_font_size, 0, date("Y-m-d H:i:s T", $row[0]),1);
										imagecopymerge ($image, $img_box_with_text[0], 0,$y-$img_box_with_text[2], 0, 0, $img_box_with_text[1], $img_box_with_text[2], 40);
									}
								}
								$last_time = $row[0];
								$timestamps_arr[] = $y;
							}
						if ($y == $num_rows-1 ) {
							//imagepng($image,'/var/www/signals ' . date("Y-m-d H:i:s T") . '.png');
						}
				}
				else {
					echo "something go wrong " . __LINE__;
				}
			}
			$y++;
		}
		for ($xx = 0; $xx <= count($data_array); $xx++) {
			if (($xx % 32) === 0) {	// 32 - 10 MHz
				{ // Frequency boxes painting
					if (($xx % 64) === 0) {	// 64 - 20MHz
						$img_box_with_text = make_box_with_text (array(0,0,0), array(255,255,255), $font_file, $freq_box_font_size, 0, (($freq_low + $freq_grade * $xx)/1000/1000),1);
						if ($xx === 0) {
							imagecopymerge ($image, $img_box_with_text[0], $xx,0, 0, 0, $img_box_with_text[1], $img_box_with_text[2], 40);
						}
						elseif ($xx === count($data_array)-1) {
							imagecopymerge ($image, $img_box_with_text[0], $xx-($img_box_with_text[1]),0, 0, 0, $img_box_with_text[1], $img_box_with_text[2], 40);
						}
						else {
							imagecopymerge ($image, $img_box_with_text[0], $xx-($img_box_with_text[1]/2),0, 0, 0, $img_box_with_text[1], $img_box_with_text[2], 40);
						}
					}
					if (($xx % 64) === 0) {	// 64 - 20MHz
						$img_box_with_text = make_box_with_text (array(0,0,0), array(255,255,255), $font_file, $freq_box_font_size, 0, (($freq_low + $freq_grade * $xx)/1000/1000),1);
						if ($xx === 0) {
							imagecopymerge ($image, $img_box_with_text[0], $xx,$num_rows-$img_box_with_text[2], 0, 0, $img_box_with_text[1], $img_box_with_text[2], 40);
						}
						elseif ($xx === count($data_array)-1) {
							imagecopymerge ($image, $img_box_with_text[0], $xx-($img_box_with_text[1]),$num_rows-$img_box_with_text[2], 0, 0, $img_box_with_text[1], $img_box_with_text[2], 40);
						}
						else {
							imagecopymerge ($image, $img_box_with_text[0], $xx-($img_box_with_text[1]/2),$num_rows-$img_box_with_text[2], 0, 0, $img_box_with_text[1], $img_box_with_text[2], 40);
						}
					}
				}
				if (($xx % 64) === 0) {	// 64 - 20MHz
					imagecopymerge ($image, $white_vertical_line, $xx, $img_box_with_text[2], 0, 0, 1, $num_rows-$img_box_with_text[2]*2, 30);
				}
				else {
					imagecopymerge ($image, $white_vertical_line, $xx, 0, 0, 0, 1, $num_rows, 20);
				}
			}
		}
		imagepng   ($image,'/var/www/signals-test.png', 0);
		unset ($timestamps_arr);
		echo "image created" . PHP_EOL;
		die();
	}


?>
